package com.xd.core.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xd.common.exception.XkdException;
import com.xd.core.business.dto.EditInsurerDto;
import com.xd.core.business.dto.InsurerAccountDto;
import com.xd.core.business.dto.InsurerDto;
import com.xd.core.business.entity.InsuranceCompanyInfo;
import com.xd.core.business.mapper.InsuranceCompanyInfoMapper;
import com.xd.core.business.service.IInsuranceCompanyInfoService;
import com.xd.core.business.vo.InsurerListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 投保公司信息 服务实现类
 * </p>
 *
 * @author Zch
 * @since 2025-06-18
 */
@Service
public class InsuranceCompanyInfoServiceImpl extends ServiceImpl<InsuranceCompanyInfoMapper, InsuranceCompanyInfo> implements IInsuranceCompanyInfoService {

    @Autowired
    private InsuranceCompanyInfoMapper infoMapper;

    /***
     * @description 新增投保公司
     * @author Zch
     * @date 17:21 2025/6/18 星期三
     * @return void
     **/
    @Override
    public void addInsurer(InsurerDto insurerDto) {
        if (ObjectUtils.isEmpty(insurerDto))
            throw new XkdException("参数有误");
        InsuranceCompanyInfo insuranceCompanyInfo = new InsuranceCompanyInfo();
        //查看该投保公司是否已经认证了

        BeanUtils.copyProperties(insurerDto, insuranceCompanyInfo);
        insuranceCompanyInfo.setCreateTime(LocalDateTime.now());
        insuranceCompanyInfo.setUpdateTime(LocalDateTime.now());
        this.save(insuranceCompanyInfo);
    }

    /**
     * @return com.xd.core.business.vo.InsurerListVo
     * @description 资方投保公司列表
     * @author Zch
     * @date 10:06 2025/6/19 星期四
     **/
    @Override
    public List<InsurerListVo> insurerList(Integer companyId) {
        if (StrUtil.isEmpty(companyId.toString()))
            throw new XkdException("参数错误");
        return infoMapper.insurerList(companyId);
    }

    /***
     * @description 编辑投保公司数据回显
     * @author Zch
     * @date 10:37 2025/6/19 星期四
     * @return com.xd.core.business.vo.InsurerListVo
     **/
    @Override
    public InsurerListVo insurerOne(Integer insurerId) {
        InsurerListVo insurerListVo = new InsurerListVo();
        if (StrUtil.isEmpty(insurerId.toString()))
            throw new XkdException("参数错误");
        InsuranceCompanyInfo insuranceCompanyInfo = this.getById(insurerId);
        if (ObjectUtil.isNotEmpty(insuranceCompanyInfo)) {
            BeanUtils.copyProperties(insuranceCompanyInfo, insurerListVo);
        }
        return insurerListVo;
    }

    /***
     * @description 编辑投保公司信息
     * @author Zch
     * @date 10:40 2025/6/19 星期四
     * @return void
     **/
    @Override
    public void editInsurer(EditInsurerDto editInsurerDto) {
        if (ObjectUtil.isEmpty(editInsurerDto))
            throw new XkdException("参数错误");
        InsuranceCompanyInfo info = this.getById(editInsurerDto.getId());
        if (ObjectUtil.isNotEmpty(info)) {
            BeanUtils.copyProperties(editInsurerDto, info);
            info.setUpdateTime(LocalDateTime.now());
        }
        this.updateById(info);
    }

    /***
     * @description 查看投保公司账户信息
     * @author Zch
     * @date 11:01 2025/6/19 星期四
     * @return com.xd.core.business.dto.InsurerAccountDto
     **/
    @Override
    public InsurerAccountDto insurerAccount(Integer insurerId) {
        if (StrUtil.isEmpty(insurerId.toString()))
            throw new XkdException("参数错误");
        return infoMapper.insurerAccount(insurerId);
    }

    /***
     * @description 修改投保机构启用状态
     * @author Zch
     * @date 11:12 2025/6/20 星期五
     * @return void
     **/
    @Override
    public void editInsurerEnable(Integer isEnable, Integer insurerId) {
        if (StrUtil.isEmpty(insurerId.toString()) || StrUtil.isEmpty(isEnable.toString()))
            throw new XkdException("参数错误");
        InsuranceCompanyInfo info = this.getById(insurerId);
        if (ObjectUtil.isNotEmpty(info)) {
            info.setIsEnable(isEnable);
            info.setUpdateTime(LocalDateTime.now());
            this.updateById(info);
        }
    }
}
